<?php
/**
 * 功能描述：	通用文章管理
 * @datetime	2022-07-12
 * @version   	v2.3
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_EssayModel extends Model {

    /**
     * Notes:列表总数-通用主附表关联
     * User: ZhuHaili
     */
    function gerEssayCount($mouldCode, $menuId, $fid, $whereAdd){
        if(isset($whereAdd['where']) && $whereAdd['where']){
            $where = $whereAdd['where'];
            unset($whereAdd['where']);
        }
        $where['a.menu_id'] = $menuId;
        $where['a.fid'] = $fid;
        $sql = $whereAdd;
        $sql['where'] = $where;
        $sql['field'] = 'count(1) counts';
        $sql['table'] = $mouldCode;
        $sql['as'] = 'a';
        $result = $this->conn('mysql')->find($sql);
        return isset($result['counts']) ? $result['counts'] : 0;
    }

    /**
     * Notes:列表信息分页数据-通用主附表关联
     * User: ZhuHaili
     */
    function getEssayList($mouldCode, $menuId, $fid, $whereAdd, $start, $limit, $orderby){
        if(isset($whereAdd['where']) && $whereAdd['where']){
            $where = $whereAdd['where'];
            unset($whereAdd['where']);
        }
        $where['a.menu_id'] = $menuId;
        $where['a.fid'] = $fid;
        $sql = $whereAdd;
        $sql['where'] = $where;
        $sql['field'] = '*';
        $sql['table'] = $mouldCode;
        $sql['as'] = 'a';
        $sql['order'] = $orderby ? $orderby : 'a.viewseq desc,a.createdate desc,a.id desc';
        $sql['limit']['start'] = $start;
        $sql['limit']['end'] = $limit;
        $r = $this->conn('mysql')->select($sql);
        return $r;
    }

    /**
     * Notes: 列表-导出-通用主附表关联
     * User: ZHL
     * Date: 2020/10/11
     */
    function getEssayLists($mouldCode, $menuId, $fid, $whereAdd, $start, $limit, $orderby){
        if(isset($whereAdd['where']) && $whereAdd['where']){
            $where = $whereAdd['where'];
            unset($whereAdd['where']);
        }
        $where['a.menu_id'] = $menuId;
        $where['a.fid'] = $fid;
        $sql = $whereAdd;
        $sql['where'] = $where;
        $sql['field'] = "a.*";
        $sql['table'] = $mouldCode;
        $sql['as'] = 'a';
        $sql['order'] = $orderby ? $orderby : 'a.viewseq desc,a.id desc,a.createdate desc';
        if($limit){
            $sql['limit']['start'] = $start;
            $sql['limit']['end'] = $limit;
        }
        return $this->conn('mysql')->select($sql);
    }

    /**
     * Notes: 文章属性条数统计
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    public function getEssayElementCount($mouldCode, $essayId, $essayField){
        $where['essay_id'] = $essayId;
        $where['essay_field'] = $essayField;
        $sql['where'] = $where;
        $sql['field'] = 'count(1) counts';
        $sql['table'] = $mouldCode;
        $result = $this->conn('mysql')->find($sql);
        return isset($result['counts']) ? $result['counts'] : 0;
    }

    /**
     * Notes:文章属性列表
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    public function getEssayElementList($mouldCode, $essayId, $essayField, $start, $limit){
        $where['essay_id'] = $essayId;
        $where['essay_field'] = $essayField;
        $sql['where'] = $where;
        $sql['field'] = '*';
        $sql['table'] = $mouldCode;
        $sql['order'] = 'viewseq desc,id desc,createdate desc';
        $sql['limit']['start'] = $start;
        $sql['limit']['end'] = $limit;
        return $this->conn('mysql')->select($sql);
    }
}